package com.liyy.platform.controller;

import com.liyy.platform.domain.ContractProxySupply;
import com.liyy.platform.domain.ContractProxySupplyItem;
import com.liyy.platform.domain.ContractProxySupplyItemPrice;
import com.liyy.platform.service.ContractProxySupplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

@RestController
@RequestMapping("/search/contractProxySupply")
public class ContractProxySupplyController {

    private final Logger logger=Logger.getLogger(String.valueOf(getClass()));
    @Autowired
    private ContractProxySupplyService contractProxySupplyService;
    @Autowired
    private DiscoveryClient discoveryClient;//用来从服务注册中心获取服务的相关信息

    //用于测试添加数据，实际数据通过rabbitMQ监听程序自动添加进ES
    @GetMapping("/saveContractProxySupply")
    public void saveContractProxySupply(){
        ContractProxySupply contractProxySupply=new ContractProxySupply();
        contractProxySupply.setId(1L);
        contractProxySupply.setSupplierId(1L);
        contractProxySupply.setSupplierEntName("黑人有限公司111");
        contractProxySupply.setDateEffectiveStart(new Date(2017,12,12));
        contractProxySupply.setDateEffectiveEnd(new Date());
        ContractProxySupplyItem item=new ContractProxySupplyItem();
        item.setId(1L);
        item.setCateName("进口用品111");
        item.setGoodsUnit("元");
        item.setGoodsName("黑人牙膏111");
        item.setIsFreight(1);
        item.setIsFax(1);
        item.setQuantitySupply(1000.0);
        item.setGoodsBrand("黑人牌111");
        item.setGoodsAttr("清新防蛀牙111");
        ContractProxySupplyItemPrice price=new ContractProxySupplyItemPrice();
        price.setId(1L);
        price.setQuantityStart(100.0);
        price.setQuantityEnd(200.0);
        price.setUnitPrice(96.0);
        ContractProxySupplyItemPrice price2=new ContractProxySupplyItemPrice();
        price2.setId(123L);
        price2.setQuantityStart(200.0);
        price2.setQuantityEnd(500.0);
        price2.setUnitPrice(109.0);
        List l1=new ArrayList();
        l1.add(price);
        l1.add(price2);
        item.setItemPriceList(l1);
        List l2=new ArrayList();
        l2.add(item);
        contractProxySupply.setItemList(l2);
        contractProxySupplyService.saveContractProxySupply(contractProxySupply);
    }
    //用于测试删除数据，实际数据通过rabbitMQ监听程序自动添加进ES
    @GetMapping("/delContractProxySupply")
    public void delContractProxySupply(long id){
        contractProxySupplyService.deleteContractProxySupply(id);
    }


//代理合同的查询及分页
    @GetMapping("/getContractProxySupplyList")
    public List getContractProxySupplyList(String goodsName, String goodsBrand, String goodsAttr,
                                                 @RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize ){
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        logger.info("本次服务的 Host:"+instance.getHost()+","+"Port:"+instance.getPort()+","+"ServiceId:"+instance.getServiceId());
        return contractProxySupplyService.getContractProxySupplyList(goodsName,goodsBrand,goodsAttr,pageNo,pageSize);
    }

    @PostMapping("/testBody")
    public Map testBody(@RequestBody Map map){
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        logger.info("本次服务的 Host:"+instance.getHost()+","+"Port:"+instance.getPort()+","+"ServiceId:"+instance.getServiceId());
        System.out.println(map);
        return map;
    }

}
